﻿@inherits TscComponentBase

@if (Value.ListType == ListTypes.TopList)
{
    <TopList Value=Value Class="@Class" />
}
else
{
    <Table Value="Value" Class="@Class" />
}

@code {
    ListTypes? _lastListType { get; set; }

    string? _oldKey;

    bool _shouldRender;

    string jumpId { get; set; }

    string jumpName { get; set; }

    List<string> services { get; set; } = new();

    [Parameter]
    public ITablePanelValue Value { get; set; }

    [CascadingParameter]
    public ConfigurationRecord ConfigurationRecord { get; set; }

    [Parameter]
    public bool IsChartDataReady { get; set; }

    protected override async Task OnParametersSetAsync()
    {
        var keyChanged = _oldKey != ConfigurationRecord.Key;
        var listTypeChanged = _lastListType != Value.ListType && Value.ListType != ListTypes.TopList;
        if (keyChanged || listTypeChanged)
        {
            _lastListType = Value.ListType;
            _oldKey = ConfigurationRecord.Key;
            if (listTypeChanged)
            {
                jumpId = await GetJumpId();
                jumpName = GetJumpName();
            }
            if (keyChanged || listTypeChanged)
            {
                services = await GetServiceNames() ?? new();               
            }
            _shouldRender = true;
        }

        //if (IsChartDataReady && _shouldRender)
        //{
        //    _shouldRender = false;
        //    SetOption();
        //}
        if (IsChartDataReady)
        {
            SetOption();
        }
    }

    void SetOption()
    {
        if (Value.ListType is ListTypes.TopList)
        {
            Value.SetTopListOption(jumpId);
        }
        else
        {
            Value.SetTableOption(services, jumpName, jumpId);
        }
    }

    async Task<List<string>> GetServiceNames()
    {
        return await base.ApiCaller.MetricService.GetValues(new RequestMetricListDto
        {
            Service = ConfigurationRecord.Service,
            Instance = ConfigurationRecord.Instance,
                Endpoint = ConfigurationRecord.Endpoint,
            Layer = ConfigurationRecord.Layer,
            Type = GetMetricValueType()
        });
    }

    async Task<string> GetJumpId()
    {
        var data = await base.ApiCaller.InstrumentService.GetLinkAsync(ConfigurationRecord.Layer, GetMetricValueType());
        return data.InstrumentId?.ToString() ?? "";
    }

    MetricValueTypes GetMetricValueType()
    {
        MetricValueTypes type = default;
        switch (Value.ListType)
        {
            case ListTypes.ServiceList:
                type = MetricValueTypes.Service;
                break;
            case ListTypes.InstanceList:
                type = MetricValueTypes.Instance;
                break;
            case ListTypes.EndpointList:
                type = MetricValueTypes.Endpoint;
                break;
            case ListTypes.TopList:
                type = MetricValueTypes.TopList;
                break;
            default:
                break;
        }

        return type;
    }

    string GetJumpName()
    {
        var jumpName = "";
        switch (Value.ListType)
        {
            case ListTypes.ServiceList:
                jumpName = "service_name";
                break;
            case ListTypes.InstanceList:
                jumpName = "exported_instance";
                break;
            case ListTypes.EndpointList:
                jumpName = "http_target";
                break;
            default:
                break;
        }
        return jumpName;
    }
}
